package com.bcbim.api_web.ability.controller;

import com.bcbim.authentication_interface.ability.dao.RoleAbilityDAO;
import com.bcbim.authentication_interface.ability.dto.AbilityDTO;
import com.bcbim.authentication_interface.ability.service.AbilityService;
import com.bcbim.authentication_interface.ability.vo.AbilityDetailVO;
import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Code;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.common.util.ParamCheck;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: working_platform
 * @Description:
 * @Author xuehao
 * @Date 2022/02/09  15:19
 * @Version V1.0
 **/
@RequestMapping("/ability")
@RestController
public class AbilityController {

    @Reference(protocol = "dubbo", group = "ability", version = "1.0.0")
    private AbilityService abilityServiceImpl;

    /**
     * 二级菜单下拉
     *
     * @param parentId
     * @author xuehao
     * @createDate 2022/2/9 15:22
     * @retuen ReturnMsg
     */
    @PostMapping("/pullSubMenu")
    public ReturnMsg pullSubMenu(String parentId, String applicationPlatform) {
        try {
            ParamCheck.idCheck(parentId);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        List<Code> list = abilityServiceImpl.pullSubMenu(parentId, applicationPlatform);
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 一级菜单下拉
     *
     * @author xuehao
     * @createDate 2022/2/10 10:37
     * @retuen ReturnMsg
     */
    @PostMapping("/pullParentMenu")
    public ReturnMsg pullParentMenu(String applicationPlatform) {
        try {
            ParamCheck.stringNotEmpty(applicationPlatform, Constant.CODE_LENGTH_ONE);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        List<Code> list = abilityServiceImpl.pullParentMenu(applicationPlatform);
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 新增功能
     *
     * @param abilityDTO
     * @author xuehao
     * @createDate 2022/2/10 16:28
     * @retuen ReturnMsg
     */
    @PostMapping("/insertAbility")
    public ReturnMsg insertAbility(AbilityDTO abilityDTO) {
        //参数校验
        try {
            ParamCheck.idCheck(abilityDTO.getMenuId());
            ParamCheck.stringCanBeEmpty(abilityDTO.getSubMenu(), 32);
            ParamCheck.stringNotEmpty(abilityDTO.getAbilityName(), 10);
            ParamCheck.stringNotEmpty(abilityDTO.getAbilityUrl(), 50);
            ParamCheck.stringNotEmpty(abilityDTO.getAbilityId(), 30);
            ParamCheck.stringCanBeEmpty(abilityDTO.getIcon(), 50);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //整理参数
        if (StringUtils.isNotEmpty(abilityDTO.getSubMenu())) {
            abilityDTO.setMenuId(abilityDTO.getSubMenu());
        }
        String id = GuidUtil.get();
        String abilityId = GuidUtil.get();
        //获取角色id
        String roadId = abilityServiceImpl.getRoleIdByUser(AuthUtil.getUserIdDecrypt());
        RoleAbilityDAO roleAbilityDAO = new RoleAbilityDAO();
        roleAbilityDAO.setId(id);
        roleAbilityDAO.setRoleId(roadId);
        roleAbilityDAO.setAbilityId(abilityId);
        abilityServiceImpl.insertRoleAbility(roleAbilityDAO);
        abilityDTO.setId(abilityId);
        abilityDTO.setCreateBy(AuthUtil.getUserIdDecrypt());
        abilityDTO.setCreateTime(LocalDateTime.now());
        abilityDTO.setEnable(Constant.ENABLE);
        abilityDTO.setDel(Constant.NOT_DEL);
        int result = abilityServiceImpl.insertAbility(abilityDTO);
        return result == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }

    /**
     * 删除功能
     *
     * @param id
     * @author xuehao
     * @createDate 2022/2/10 17:39
     * @retuen ReturnMsg
     */
    @PostMapping("/deleteAbility")
    public ReturnMsg deleteAbility(String id) {
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        int result = abilityServiceImpl.deleteAbility(id);
        return result == 1 ? ReturnMsg.consSuccess(Constant.DEL_SUCCESS) : ReturnMsg.consFail(Constant.DEL_FAIL);
    }

    /**
     * 查询功能详情
     *
     * @param abilityId
     * @author xuehao
     * @createDate 2022/2/11 15:11
     * @retuen ReturnMsg
     */
    @PostMapping("/getAbilityDetail")
    public ReturnMsg getAbilityDetail(String abilityId) {
        try {
            ParamCheck.idCheck(abilityId);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        AbilityDetailVO abilityDetailVO = abilityServiceImpl.getAbilityDetail(abilityId);
        if (ObjectUtils.isNotEmpty(abilityDetailVO)) {
            String parentId = abilityServiceImpl.getParentMenuId(abilityDetailVO.getMenuId());
            if (StringUtils.isNotEmpty(parentId)) {
                abilityDetailVO.setParentId(parentId);
            }
        }
        return ReturnMsg.consSuccess(abilityDetailVO);
    }

    /**
     * 编辑功能
     *
     * @param abilityDTO
     * @author xuehao
     * @createDate 2022/2/11 16:54
     * @retuen ReturnMsg
     */
    @PostMapping("/updateAbility")
    public ReturnMsg updateAbility(AbilityDTO abilityDTO) {
        //参数校验
        try {
            ParamCheck.idCheck(abilityDTO.getId());
            ParamCheck.stringNotEmpty(abilityDTO.getSubMenu(), 32);
            ParamCheck.stringNotEmpty(abilityDTO.getAbilityName(), 10);
            ParamCheck.stringNotEmpty(abilityDTO.getAbilityUrl(), 50);
            ParamCheck.stringNotEmpty(abilityDTO.getAbilityId(), 30);
            ParamCheck.stringCanBeEmpty(abilityDTO.getIcon(), 50);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //整理参数
        abilityDTO.setUpdateBy(AuthUtil.getUserIdDecrypt());
        abilityDTO.setUpdateTime(LocalDateTime.now());
        int result = abilityServiceImpl.updateAbility(abilityDTO);
        return result == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }
}

